import Slot from "../../resources/prefabs/Slot";
import NodeUtil from "../framework/utils/NodeUtil";
import { GData } from "../framework/utils/GData";
import { PetData } from "./Entity/PetData";
import EventManager from "../framework/core/EventManager";
import { GameEvent } from "../constants/Events";
import { SPINE_ACTION } from "../framework/utils/Define";
import Fighter from "../../resources/prefabs/Fighter";
import BattleView from "./BattleView";

const {ccclass, property} = cc._decorator;

const GRID_ROW_NUM = 2   
const GRID_COL_NUM = 5;    

@ccclass
export default class Game extends cc.Component {

    @property(cc.Node)
    battleScene:cc.Node = null;
    
    @property(cc.Node)
    posLayer:cc.Node = null;

    @property(cc.Prefab)
    slotPrefab:cc.Prefab = null;
    //节点位置
    slotNode:cc.Node[] = [];
     

    battleViewCom:BattleView = null;

    onLoad(){
        this.reg(); 
        this.battleViewCom =  this.battleScene.getComponent(BattleView)
        this.init();
    }
  
    onDestroy(){
        this.unreg();
    }
 
    init(){
        if(GData.Slots.length <=0 ){ 
            for (let index = 0; index < 10; index++) {
                let pet = new PetData(index,index);
                GData.Slots.push(pet);
            } 
        }
        GPool.init();
        this.initSlots();
    }

    reg(){ 
        EventManager.on(GameEvent.ADD_FIGHT, this.addFightHome, this);    
    }

    unreg(){
        this.node.off(GameEvent.ADD_FIGHT, this.addFightHome, this);    
    }

    //加入战场
    addFightHome(pos,info){
        GPool.get("Fighter",{setData:{game:this,data:info}}).then((node:cc.Node)=>{
            this.battleViewCom.addFighterLayer(node,pos)
        });
    }

    //检查是否进入战场 如果不是进入战场就还原位置
    exchangeFighterMovelLogic(srcSlot:Slot){  
        let pos  = srcSlot.fighter.convertToWorldSpaceAR(cc.v2(0.5,0.5)) 
        for (const key in this.slotNode) {  
            let node:cc.Node = this.slotNode[key];
            let slotComp:Slot = node.getComponent("Slot");
            if(NodeUtil.isPosOnNodeRect(cc.v2(pos.x,pos.y),node)){ 
                let l = srcSlot.idx
                let r = slotComp.idx 
                //交换两个都有
                if(srcSlot.data && slotComp.data){
                    var temp = srcSlot.data;
                    srcSlot.data = slotComp.data;
                    slotComp.data = temp; 
                    slotComp.updateSlot(); 
                    srcSlot.updateSlot();
                }else{ 
                    slotComp.data = srcSlot.data; 
                    slotComp.updateSlot(); 
                    srcSlot.clearFighter();
                }
                return;
            }
        }
        //复位
        srcSlot.resetFighterPos();  
    } 

    //检查是否回归占位 如果占位有战机就交换 没有就直接回战场  否则就是换战场位置
    checkRecoverySlot(v3:cc.Vec3){ 
        let slotComp:Slot  = null;
        for (const key in this.slotNode) { 
            const slotNode =  this.slotNode[key]; 
            let isContain = NodeUtil.isPosOnNodeRect(cc.v2(v3.x,v3.y), slotNode);
            if(isContain){ 
                slotComp = slotNode.getComponent(Slot);
                break;
            } 
        }  
        return slotComp;
    }
 
    //初始化占位
    initSlots(){
        let size = this.posLayer.getContentSize()
        let w = size.width/GRID_COL_NUM
        let h = size.height/GRID_ROW_NUM +20
        for (let r = 0; r < GRID_ROW_NUM; r++) {
            for (let c = 0; c < GRID_COL_NUM; c++) {
                var idx = r*GRID_COL_NUM+c; 
                GPool.get("Slot",{setData:{obj:this,idx:idx,data:GData.Slots[idx]}}).then((slot: cc.Node) => {
                    slot.parent = this.posLayer;
                    slot.x = w*c - (size.width-slot.width)*0.5 + (w-slot.width)*0.5; 
                    slot.y = h*(GRID_ROW_NUM-r) -(size.height) + (h-slot.height);
                    this.slotNode.push(slot);
               }); 
            }
        }
    }

    // update (dt) {}
}
